﻿/**====================================
 *Copyright(C) 2018 by Wipace 
 *All rights reserved. 
 *FileName:     .cs 
 *Author:       CGzhao 
 *Version:      1.0 
 *UnityVersion：2018.2.3 
 *Date:         2018-11-27 
 *Email:		1341674064@qq.com
 *Description:    
 *History: 
======================================**/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBundles : MonoBehaviour
{
	private const string path = "AssetBundles/";
	// Use this for initialization
	void Start () {
//		//加载材质ab包
//		AssetBundle material = AssetBundle.LoadFromFile(path + "material_ground");
//		
//		//加载预制体ab包
//		AssetBundle ab = AssetBundle.LoadFromFile(path + "cube");
//		GameObject wall = ab.LoadAsset<GameObject>("cube");
//		Instantiate(wall);
		
		//使用异步加载ab包
		//StartCoroutine("LoadAssetBundleAsync");
		
		//使用WWW加载本地的assetbundle
		StartCoroutine("LoadAssetBundlesFunc");
	}

	IEnumerator LoadAssetBundlesFunc()
	{
		//加载本地的文件url
		//string url = @"file:///D:\unityPrograms\unity_AssetsBundleExample\AssetsBundleExample\AssetBundles\cube";
		//加载服务器的文件url
		string url = @"http://localhost/AssetBundles/cube";
		//从服务器下载
		WWW request = new WWW(url);
		yield return request;
		if (string.IsNullOrEmpty(request.error) == false)
		{
			Debug.Log(request.error);
			yield break;
		}

		AssetBundle ab = request.assetBundle;
		Instantiate(ab.LoadAsset<GameObject>("cube"));
	}

	IEnumerator LoadAssetBundleAsync()
	{
		AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path + "cube");
		yield return request;
		Debug.Log(request.isDone);
		AssetBundle ab = request.assetBundle;
		Instantiate(ab.LoadAsset<GameObject>("cube"));
	}
}
